#!/bin/bash
# PostgreSQL 数据库备份脚本

set -e

BACKUP_DIR="./backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_FILE="$BACKUP_DIR/skywalking_backup_$TIMESTAMP.sql"

# 创建备份目录
mkdir -p "$BACKUP_DIR"

echo "============================================"
echo "PostgreSQL 数据库备份"
echo "============================================"
echo ""
echo "备份文件: $BACKUP_FILE"
echo ""

# 执行备份
docker exec skywalking-postgres pg_dump -U skywalking skywalking > "$BACKUP_FILE"

# 压缩备份文件
gzip "$BACKUP_FILE"

echo ""
echo "✅ 备份完成: ${BACKUP_FILE}.gz"
echo ""
echo "📊 备份文件大小: $(du -h ${BACKUP_FILE}.gz | cut -f1)"
echo ""

# 清理超过 30 天的备份
find "$BACKUP_DIR" -name "*.sql.gz" -mtime +30 -delete
echo "🗑️  已清理超过 30 天的旧备份"
echo ""

